package com.lzx.kafka.helper;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.lzx.kafka.model.Notify;
import com.taobao.api.ApiException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * @ClassName DingDingHelper
 * @Author 刘正星
 * @Date 2021/4/27 上午10:05
 * @Description
 */
@Component
public class DingDingHelper implements NotifyHelper {
    @Value("${dewu.notify.dingding}")
    private String dingdingUrl;


    // #0. 配置钉钉客户端，dingdingUrl即根据刚才拷贝的Webhook



    /**
     * 发送钉钉消息
     * @param notify 通知
     * @return 发送是否成功
     */
    @Override
    public boolean sendNotify(Notify notify){

        //TODO 根据类型调用下面的方法

        switch (notify.getNotifyType()) {
            case "text":
                return sendText(notify);
            case "link":
                return sendLink(notify);
            case "markdown":
                return sendMarkdown(notify);
        }

        /*if ("text".equals(notify.getNotifyType())) {
            return  this.sendText(notify);
        }
        if ("link".equals(notify.getNotifyType())) {
            return this.sendLink(notify);
        }
        if ("markdown".equals(notify.getNotifyType())) {
            return this.sendMarkdown(notify);
        }*/
        return false;
    }

    /**
     * 发送文字通知
     * @param notify 通知
     * @return 发送是否成功
     */
    public boolean sendText(Notify notify){

        //client为啥不可以定义为成员变量呢？
        DingTalkClient client = new DefaultDingTalkClient(this.dingdingUrl);

        // #1. request表示整个消息请求
        OapiRobotSendRequest request = new OapiRobotSendRequest();
        // #2. 请求设置消息类别
        request.setMsgtype("text");
        // #3. 设置消息内容
        OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
        text.setContent(notify.getText());
        request.setText(text);

        // #4. 设置钉钉@功能
        OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
        at.setIsAtAll(true);
        request.setAt(at);
        try {
            // #5. 发送消息请求
            client.execute(request);
        } catch (ApiException e) {
            e.printStackTrace();
        }

        return true;
    }

    /**
     * 发送链接通知
     * @param notify 通知
     * @return 发送是否成功
     */
    public boolean sendLink(Notify notify){
        DingTalkClient client = new DefaultDingTalkClient(this.dingdingUrl);
        OapiRobotSendRequest request = new OapiRobotSendRequest();
        request.setMsgtype("link");
        OapiRobotSendRequest.Link link = new OapiRobotSendRequest.Link();
        link.setMessageUrl(notify.getLink());
        link.setPicUrl(notify.getPicLink());
        link.setTitle(notify.getTitle());
        link.setText(notify.getText());
        request.setLink(link);
        try {
            client.execute(request);
        } catch (ApiException e) {
            e.printStackTrace();
        }
        return true;
    }

    /**
     * 发送Markdown通知
     * @param notify 通知
     * @return 发送是否成功
     */
    public boolean sendMarkdown(Notify notify){
        DingTalkClient client = new DefaultDingTalkClient(this.dingdingUrl);
        OapiRobotSendRequest request = new OapiRobotSendRequest();
        request.setMsgtype("markdown");
        OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown();
        markdown.setTitle(notify.getTitle());
        markdown.setText(notify.getTitle());
        request.setMarkdown(markdown);
        try {
            client.execute(request);
        } catch (ApiException e) {
            e.printStackTrace();
        }
        return true;
    }
}
